package com.winchain.chainbill.bill.biz.transfer.cmd;

import com.shell.bee.acct.AcctException;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.command.CommandContext;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.send.cmd.TxCmd;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillXfeTranCmd extends TxCmd<CbXfe> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public boolean execute(CommandContext<Bill<CbXfe>> context) throws Exception {
		boolean timeout = ((Boolean) context.getValue("Timeout")).booleanValue();
		if (!timeout) {
			AcctTxRec tran = getAcctTxRec(context);
			this.tradeService.fire(tran);

			context.setValue("txSn", tran.getTxSn());
		}
		return false;
	}

	private AcctTxRec getAcctTxRec(CommandContext<Bill<CbXfe>> context) {
		CbRec record = ((Bill) context.getSource()).getRec();

		CbXfe cbXfe = (CbXfe) ((Bill) context.getSource()).getSource();
		String billId = record.getBillId();

		String buyerId = cbXfe.getBuyerId();
		BigDecimal billAmt = cbXfe.getBillAmt();

		Object bizType = context.getValue("bizType");
		if (bizType == null) {
			throw new AcctException("������������������");
		}
		String abs = "";
		String creditAcctUse = "";
		String acctUse = "";
		if ("cb_xfe_rev".equals(bizType)) {
			abs = "��������������";
			creditAcctUse = "1";

			acctUse = "2";
		}
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());
		txRec.setCurrCode(AssetCode.CNY.name());
		txRec.setTxChnCode("C");

		AcctBas creditAcct = getAcctBas(buyerId, creditAcctUse, null);

		String tranAcctNo = creditAcct.getAcctNo();
		txRec.setTxAcctNo(tranAcctNo);
		txRec.setTxAmt(billAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxType((String) bizType);

		txRec.setTxSts("I");

		AcctBas acct = getAcctBas(buyerId, acctUse, null);
		txRec.setCpAcctNo(acct.getAcctNo());
		txRec.setCpAcctNm(acct.getAcctNm());
		txRec.setCpBankNm(acct.getFiNm());
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(billId);
		txRec.setDataScLvl("B");

		AcctBkp txAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getTxAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setTxBkpId(txAcctBkp.getBkpId());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getCpAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setCpBkpId(cpAcctBkp.getBkpId());
		return txRec;
	}
}
